package cn.edu.cugb.xvideoplayer.activity;

import cn.edu.cugb.xvideoplayer.R;
import cn.edu.cugb.xvideoplayer.controller.MymediaController;
import cn.edu.cugb.xvideoplayer.view.VideoView;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

/** Created by wuyexiong on 13-9-1. */
public class YoutubeVideoActivity extends Activity {

    private MymediaController mMediaController;
    private VideoView mVideoView;
    private LinearLayout mVideoWraper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.youtube_video);
        mVideoWraper = (LinearLayout) findViewById(R.id.video_wrap);
        mVideoView = (VideoView) findViewById(R.id.youtube_view);
        mMediaController = new MymediaController(this);
        mVideoView.setVideoPath("/sdcard/wonder.mp4");
        mVideoView.setMediaController(mMediaController);
        mVideoView.requestFocus();
    }


    public static void launch(Context context) {
        Intent intent = new Intent(context, YoutubeVideoActivity.class);
        context.startActivity(intent);
    }
    
	private void setFullscreen(boolean on) {
		Window win = getWindow();
		WindowManager.LayoutParams winParams = win.getAttributes();
		final int bits = WindowManager.LayoutParams.FLAG_FULLSCREEN;
		if (on) {
			winParams.flags |= bits;
		} else {
			winParams.flags &= ~bits;
		}
		win.setAttributes(winParams);
	}
    
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
			setFullscreen(true);
			
			LayoutParams layoutParams = (LayoutParams) mVideoWraper.getLayoutParams();
			layoutParams.width = LayoutParams.MATCH_PARENT;
			layoutParams.height = LayoutParams.MATCH_PARENT;
			mVideoWraper.setLayoutParams(layoutParams);
			mVideoWraper.requestLayout();
			
			if (mVideoView != null) {
				mVideoView.setVideoLayout(VideoView.VIDEO_LAYOUT_STRETCH, 0);
			}
		}
		super.onConfigurationChanged(newConfig);
	}
}
